package cn.iocoder.yudao.module.bpm.service.hr;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.HrCreateReqVo;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 雨同我
 * @Description:
 * @DateTime: 2024/8/29 15:23
 **/


public class HrCommonUtils {

    String url="http://10.16.3.90:8068/sys/splc/get_LCUSER";

    //    "startUserSelectTasks":[{"id":"Activity_1xcqbes","name":"人事审批"}]}
//    @GetMapping("/hu")
//    public String testHu(){
//        JSONObject oa_leave = getProcessDefinition(null, "oa_leave");
//        Map map= (Map)oa_leave.get("data");
//        List list= (List)map.get("startUserSelectTasks");
//        Map<String,String> map1=(Map) list.get(0);
//        System.out.println("get=> "+oa_leave);
//
//        Map<String, List<Long>> tmap = new HashMap<String, List<Long>>(){{
//            put(map1.get("id"),Arrays.asList(1L));
//        }};
//        System.out.println("tmap "+tmap);
//        HrCreateReqVo vo = new HrCreateReqVo("1","oa_leave",1L,tmap);
//        JSONObject model = createCommonModel(vo);
//        System.out.println("model:= "+model);
//        return "ok";
//    }




    /**
     * @Description: 获取用户信息
     * @Author: 雨同我
     * @DateTime: 2024/8/29 15:29
     * @param: null:
     * @return:
    */
    public static AdminUserRespDTO getExternalUser(String searchdata, String searchkey){
        String url = "http://10.16.3.90:8068/sys/splc/get_LCUSER";
        Map<String, String> getHeaders = new HashMap<String, String>(){{
//           put("Authorization", "Bearer your_token");
//            put("tenant-id","1");
            put("Content-Type", "application/json");
        }};
        JSONObject body = new JSONObject();
//        TODO里面的key根据实际要求
        body.set("searchdata", searchdata);
        body.set("searchkey", searchkey);

        JSONObject getResponse = sendPostAndGetRequest(url, body,getHeaders,"post");

        List list=(List)getResponse.get("rows");
        AdminUserRespDTO startUser=new AdminUserRespDTO();

        if (ObjUtil.isNotEmpty(list)){
            Map<String,String> map=(Map)list.get(0);
            String userId = map.get("USERCODE");
            String username = map.get("USERNAME");
            String department = map.get("department");
            System.out.println(map);
            startUser.setId(NumberUtils.parseLong(userId));
            startUser.setNickname(username);
            startUser.setDeptName(department);
        }


        return startUser;
    }




    /**
     * @Description: 获取流程定义，获取发起人自选的用户节点
     * @Author: 雨同
     */
    public static JSONObject getProcessDefinition(String id, String key){
        String getUrl = "http://localhost:48080/admin-api/bpm-common/get";
        Map<String, String> getHeaders = new HashMap<String, String>(){{
//           put("Authorization", "Bearer your_token");
            put("tenant-id","1");
            put("Content-Type", "application/json");
        }};
        JSONObject getBody = new JSONObject();
//        TODO里面的key根据实际要求
        getBody.set("key", key);
        JSONObject getResponse = sendPostAndGetRequest(getUrl, getBody,getHeaders,"get");
        return getResponse;
    }

    /**
     * @Description: 创建流程
     */

    public static JSONObject createCommonModel(HrCreateReqVo hrCreateReqVo){
        // POST请求示例
        String postUrl = "http://localhost:48080/admin-api/bpm-common/create";
        JSONObject postBody = new JSONObject();
        HashMap<String, List<Long>> map = new HashMap<String, List<Long>>(){{
            put("Activity_0glwzx9", Arrays.asList(1L));
        }};

        postBody.set("serviceId", hrCreateReqVo.getBizId());
        postBody.set("key", hrCreateReqVo.getKey());
        postBody.set("userId", hrCreateReqVo.getUserId());
        postBody.set("startUserSelectAssignees", hrCreateReqVo.getStartUserSelectAssignees());  //    发起人自选审批人 Map", example = "{taskKey1: [1, 2]}。节点id，active_"

        Map<String, String> postHeaders = new HashMap<String, String>(){{
            put("Content-Type", "application/json");
            put("tenant-id","1");
        }};

        JSONObject postResponse = sendPostAndGetRequest(postUrl, postBody, postHeaders,"post");
        return postResponse;
    }


    /**
     * 发送POST / GET 请求并返回JSON格式的响应体。
     *
     * @param url 请求的URL地址。
     * @param body 请求的JSON格式的请求体。
     * @param headers 请求头参数。
     * @return JSONObject 响应的JSON对象。
     */
    public static JSONObject sendPostAndGetRequest(String url, JSONObject body, Map<String, String> headers,String type) {

        if (type.equalsIgnoreCase("get")){
            HttpRequest request = HttpRequest.get(url)
                    .addHeaders(headers);
            request.form(body);
            HttpResponse response = request.execute();
            return JSONUtil.parseObj(response.body());
        }else if (type.equalsIgnoreCase("post")){
            HttpRequest request = HttpRequest.post(url)
                    .body(body.toString())
                    .addHeaders(headers);
            HttpResponse response = request.execute();
            return JSONUtil.parseObj(response.body());
        }
        return JSONUtil.parseObj("方法给的type有问题");
    }

}
